//*****************************************************************
// PIDTransformThread.h
//
//  Created on: Jan 21, 2012
//      Author: Christopher Wood
//              Robert Livosi
//
//  $Id: PIDTransformThread.h 24 2012-01-28 02:54:38Z caw4567@gmail.com $
//*****************************************************************

#ifndef PIDTRANSFORMTHREAD_H_
#define PIDTRANSFORMTHREAD_H_

#include "TransformThread.h"

/**
 * This class is responsible for performing the PID transformation routine
 * using the input analog signal from the plant in order to drive the
 * plant at the next discrete instance in time.
 */
class PIDTransformThread : public TransformThread
{
public:
	/**
	 * Create a new PID transform thread with the specified I/O group.
	 *
	 * @param ioGroup - analog I/O thread object.
	 */
	PIDTransformThread(IOThreadGroup* signalGroup);

	/**
	 * Default destructor.
	 */
	virtual ~PIDTransformThread();

	/**
	 * Start the thread.
	 */
	void run();

	/**
	 * Retrieve the current output index.
	 *
	 * @return uIndex
	 */
	int8_t getUIndex();

	/**
	 * Retrieve the current error index.
	 *
	 * @return eIndex
	 */
	int8_t getEIndex();

	/**
	 * Advance the output index.
	 */
	void advanceUIndex();

	/**
	 * Advance the error index.
	 */
	void advanceEIndex();

	/**
	 * Retrieve a pointer to the output history buffer.
	 *
	 * @return output history buffer
	 */
	float* getUBuffer();

	/**
	 * Retrieve a pointer to the error history buffer.
	 *
	 * @return error history buffer
	 */
	float* getEBuffer();

	/**
	 * Set the Kp value.
	 *
	 * @param kp - new Kp value.
	 */
	void setKp(float kp);

	/**
	 * Set the Ki value.
	 *
	 * @param ki - new Ki value.
	 */
	void setKi(float ki);

	/**
	 * Set the Kd value.
	 *
	 * @param kd - new Kd value.
	 */
	void setKd(float kd);

	/**
	 * Set the new reference value (set point).
	 *
	 * @param ref - the new reference value.
	 */
	void setReference(float ref);

	/**
	 * Retrieve the Kp parameter.
	 *
	 * @return kp;
	 */
	float getKp();

	/**
	 * Retrieve the Ki parameter.
	 *
	 * @return ki;
	 */
	float getKi();

	/**
	 * Retrieve the Kd parameter.
	 *
	 * @return kd;
	 */
	float getKd();

	/**
	 * Retrieve the reference parameter.
	 *
	 * @return reference;
	 */
	float getReference();

	// Fixed size for history buffers (based on transform function)
	static const uint8_t OUTPUT_SIZE = 3;

private:
	// The incoming reference signal we are balancing against
	float reference;

	// PID parameters that are managed by the control thread (user input)
	float kp;
	float ki;
	float kd;

	// History buffer indices
	int8_t uIndex;
	int8_t eIndex;

	// Buffers to hold signal history
	float uBuffer[OUTPUT_SIZE];
	float eBuffer[OUTPUT_SIZE];
};

#endif /* PIDTRANSFORMTHREAD_H_ */
